package com.boot2.core.scheduler;

import com.boot2.core.dao.i.SchedulerTaskLogDao;
import com.boot2.core.model.SchedulerTask;
import com.boot2.core.model.SchedulerTaskLog;
import lombok.extern.apachecommons.CommonsLog;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@CommonsLog
public class DefaultLogJobListener implements JobListener {

    @Autowired
    SchedulerTaskLogDao schedulerTaskLogDao;

    @Override//相当于为我们的监听器命名
    public String getName() {
        return "defaultLogJobListener";
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
        try {
            log.info("执行前,jobExecutionContext");
            SchedulerTaskLog schedulerTaskLog = getSchedulerTaskLog(jobExecutionContext);
            schedulerTaskLog.setAction(1);
            schedulerTaskLog.setRemark("准备开始执行");
            schedulerTaskLogDao.saveAndFlush(schedulerTaskLog);
        } catch (Throwable e) {
            e.printStackTrace();
            log.error("定时任务日志记录异常,jobExecutionContext: " + jobExecutionContext, e);
        }
    }


    @Override//“否决JobDetail”是在Triiger被其相应的监听器监听时才具备的能力
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {
//        System.out.println("被否决执行了，可以做些日志记录。");
        try {
            log.info("被拒绝 ,jobExecutionContext");
            SchedulerTaskLog schedulerTaskLog = getSchedulerTaskLog(jobExecutionContext);
            schedulerTaskLog.setAction(2);
            schedulerTaskLog.setRemark("被拒绝执行");
            schedulerTaskLogDao.saveAndFlush(schedulerTaskLog);
        } catch (Throwable e) {
            log.error("定时任务日志记录异常,jobExecutionContext: " + jobExecutionContext, e);
        }
    }

    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext,
                               JobExecutionException jobException) {
        try {
            log.info("执行后,jobExecutionContext,jobException: " + jobException);
            SchedulerTaskLog schedulerTaskLog = getSchedulerTaskLog(jobExecutionContext);
            schedulerTaskLog.setAction(3);
            schedulerTaskLog.setRemark("成功执行结束");
            schedulerTaskLog.setEndTime(new Date());
            schedulerTaskLog.setJobRunTime(jobExecutionContext.getJobRunTime());

            //执行失败了
            if (null != jobException) {
                schedulerTaskLog.setAction(4);
                schedulerTaskLog.setRemark("执行异常");
                schedulerTaskLog.setExceptionMessage(jobException.getMessage());
            }
            schedulerTaskLogDao.saveAndFlush(schedulerTaskLog);
        } catch (Throwable e) {
            e.printStackTrace();
            log.error("定时任务日志记录异常,jobExecutionContext: " + jobExecutionContext, e);
        }
    }


    private SchedulerTaskLog getSchedulerTaskLog(JobExecutionContext jobExecutionContext) {
        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        String code = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.code);
        String bussinessId = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.bussinessId);

        SchedulerTask schedulerTask = JobExecutionContextHelper.get(code, bussinessId);

        SchedulerTaskLog schedulerTaskLog = SchedulerTaskLog.builder().
                bussinessId(bussinessId).
                code(code).
                fireTime(jobExecutionContext.getFireTime()).
                fireInstanceId(jobExecutionContext.getFireInstanceId()).
                build();
        if (null != schedulerTask) {
            schedulerTaskLog.setSchedulerTaskId(schedulerTask.getId());
            schedulerTaskLog.setCronExpression(schedulerTask.getCronExpression());
        }
        return schedulerTaskLog;
    }
}